﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MasMembers.DAL;



namespace MasMembers
{
    public partial class UserForm : Form
    {
        public LoginForm loginForm;
        public Int32 currentUser = -1;

        public UserForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UserEntity userEntity = fillEntityFromForm();
            bool savedWithNoErros = false;
            if (userEntity != null)
            {
                if (currentUser != -1)
                {
                    userEntity.Id = currentUser;
                    savedWithNoErros |= User.Update(userEntity);
                }
                else if (User.GetUserByName(userEntity.UName) == null)
                {
                    savedWithNoErros |= User.Add(userEntity);
                    loadAllUsers();
                    clearAllFields();
                }
                else
                {
                    lblerror.Text = "خطـأ .. !! هذا الاسم موجود ";
                }
                if (savedWithNoErros)
                {
                    MessageBox.Show("تم الحفظ بنجاح");
                    if (userEntity.UType == User.ADMIN_USER_UType)
                        loadAllUsers();
                }
                else
                {
                    lblerror.Text = " خطـأ لاتملك الصلاحية لهذه العملية";
                }

            }
        }

        private UserEntity fillEntityFromForm()
        {
            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                lblerror.Text = "خطـأ .. !!  كلمة المرور وتأكيد كلمة المرور غير متماثلتين ";
                return null;
            }
            else if (txtPassword.Text == "" || txtConfirmPassword.Text == "")
            {
                lblerror.Text = "خطـأ .. !! يجب ادخال كلمة المرور وتأكيد كلمة المرور ";
                return null;            
            }
            else if (cmboType.Text != User.ADMIN_USER_UType && cmboGroups.Text == "")
            {
                lblerror.Text = "خطـأ .. !! يجب اختيار مجموعة لهذا المستخدم ";
                return null;                        
            }
            else
            {
                UserEntity entity = new UserEntity();
                entity.UName = txtName.Text;
                entity.UPassword = txtConfirmPassword.Text;
                if (cmboType.Text == "")
                    entity.UType = User.NORMAL_USER_UType;
                else
                    entity.UType = cmboType.Text;

                entity.Group = GroupsNames.GetGroupByG_Name(cmboGroups.Text).G_Id;
                return entity;
            }
        }


        private void publishFormFromEntity(UserEntity user)
        {
            txtName.Text = user.UName;
            txtPassword.Text = user.UPassword;
            txtConfirmPassword.Text = user.UPassword;
            cmboType.Text = user.UType;
            cmboGroups.Text = GroupsNames.GetGroupByG_ID(user.Group).G_Name;
            currentUser = user.Id;

        }

        public void loadAllUsers()
        {
            List<UserEntity> users = User.GetAllPropUsers();
            gridUsers.DataSource = users;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UserEntity userEntity = fillEntityFromForm();

            if (userEntity != null)
            {
                if (User.GetUserByName(userEntity.UName) != null)
                {
                    if (User.Update(userEntity))
                    {
                        //Goooooooooooooooo To 
                        MainForm form = new MainForm();
                        form.Show();
                        form.loginForm = loginForm;
                        form.currentUser = User.GetUserByName(userEntity.UName);
                        this.Close();
                        this.Dispose();
                    }
                    else
                    {
                        lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود ";
                    }

                }
                else
                {
                    lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود ";
                }
            }
        }



        private void gridUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                UserEntity user = User.GetUserById(Int32.Parse(gridUsers.Rows[e.RowIndex].Cells["clmnId"].Value.ToString()));
                if (user != null)
                {
                    publishFormFromEntity(user);
                }
                else
                {
                    lblerror.Text = "لايمكن نشر هذا المستخدم";
                }
            }
        }

        private void clearAllFields()
        {
            txtConfirmPassword.Text = "";
            txtName.Text = "";
            txtPassword.Text = "";
            cmboType.Text = "";
            cmboGroups.Text = "";
            currentUser = -1;
        }

        private void ClearFields(object sender, EventArgs e)
        {
            clearAllFields();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (currentUser != -1)
            {
                DialogResult result = MessageBox.Show("", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    if (User.Delete(currentUser))
                    {
                        MessageBox.Show("تم حذف المستخدم");
                        loadAllUsers();
                    }
                }
            }
            else
            {
                lblerror.Text ="يجب اختيار مستخدم أولا";
            }
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            loginForm.Visible = true;
            this.Close();
            this.Dispose();

        }



        private void loadGroupsData()
        {
            cmboGroups.Items.Clear();
            List<GroupsNamesEntity> gList = GroupsNames.GetAllGroups();
            foreach (GroupsNamesEntity group in gList)
                cmboGroups.Items.Add(group.G_Name);
        
        }
        
        private void UserForm_Load_1(object sender, EventArgs e)
        {
            loadGroupsData();
            UserEntity user = User.GetUserById(currentUser);
            txtName.Text = user.UName;
            if (user.UType != User.ADMIN_USER_UType)
            {
                cmboGroups.Text = GroupsNames.GetGroupByG_ID(user.Group).G_Name;
                cmboType.Text = user.UType;
                btnClear.Enabled = false;
                btnDelete.Enabled = false;
                cmboType.Enabled = false;
                cmboGroups.Enabled = false;
            }
            else
            {
                btnClear.Enabled = true;
                btnDelete.Enabled = true;
                cmboType.Enabled = true;
                loadAllUsers();
            }
        }

        
    }
}
